{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<div id="tip_modal">
    <form class="layui-form p-4">
        <h3 class="pb-3">功能菜单/节点</h3>
        <table class="layui-table layui-table-form">


            <tr>
                <td class="layui-td-gray">单据日期<font>*</font>
                </td>
                <td>
                    {$detail.orderDate}
                </td>
                <td class="layui-td-gray">分摊类型
                </td>
                <td>
                    {$detail.sharType==1?'按生产数量':'按工序数量'}

                </td>
                <td class="layui-td-gray">分摊时间
                </td>
                <td> {$detail.sharTime|default=''}</td>


            </tr>
            <tr>
                <td>制单人</td>
                <td>
                    <span>{$detail.username|default=''}</span>
                </td>
                <td class="layui-td-gray">状态<font>*</font>
                </td>
                <td>
                    <select name="status" disabled>
                        {foreach [['id'=>1,'name'=>'草稿'],['id'=>2,'name'=>'已确认']] as $st}
                        <option value="{$st.id}" {if isset($detail.status)&&$detail.status==$st.id } selected {/if}>{$st.name}</option>
                        {/foreach}
                    </select>

                </td>

            </tr>
            <tr>
                <td colspan="6"><h3>数据明细</h3></td>
            </tr>
<!--            <tr>-->
<!--                <td colspan="6">-->
<!--                    {if !isset($detail.status)}-->
<!--                    <a href="javascript:" id="scWl" data-modal="{:url('addMx')}" class="layui-btn layui-btn-primary">获取数据</a>-->
<!--                    <a href="javascript:" id="gxWl" style="display: none" onclick="getGxWL()" class="layui-btn layui-btn-primary">获取数据</a>-->
<!--                    {/if}-->
<!--                    {if isset($detail.status) &&$detail.status==1}-->
<!--                    <a href="javascript:" id="scWl" {if $detail.sharType==2}style="display: none" {/if} data-modal="{:url('addMx')}" class="layui-btn layui-btn-primary">获取数据</a>-->
<!--                    <a href="javascript:" id="gxWl" {if $detail.sharType==1}style="display: none" {/if} onclick="getGxWL()" class="layui-btn layui-btn-primary">获取数据</a>-->

<!--                    {/if}-->
<!--                </td>-->
<!--            </tr>-->
            <tr>
                <table class="layui-table tableMany" id="scNum" {if $detail.sharType==2} style="display: none" {/if}>
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>物料编码</th>
                        <th>物料名称</th>
                        <th>规格型号</th>
                        <th>单位</th>
                        <th>入库数量</th>
                        <th>分摊金额</th>
                    </tr>
                    </thead>
                    <tbody id="scMxTbdy">
                    {if $detail.sharType==1}
                    {volist name='csddList' id='cdl' key='cdlkey'}
                    <tr>
                        <td>
                            <span class="xuhao">{$cdlkey}</span>
                            <input type="hidden" name="material_id[]" value="{$cdl.material_dossier_id}">
                            <input type="hidden" name="in_receipt_detail_id[]" value="{$cdl.in_receipt_detail_id}">
                            <input type="hidden" name="num[]" value="{$cdl.num}">
                        </td>
                        <td>{$cdl.mdName}</td>
                        <td>{$cdl.bar_code}</td>
                        <td>{$cdl.specs}</td>
                        <td>{$cdl.unit}</td>
                        <td>{$cdl.num}</td>
                        <td>
                            {$cdl.costPrice}
                        </td>
                    </tr>
                    {/volist}
                    {/if}
                    </tbody>
                </table>

                <table class="layui-table tableMany" id="gxNum" {if $detail.sharType==1} style="display: none" {/if}>
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>工序名称</th>
                        <th>合格数量</th>
                        <th>分摊金额</th>
                        <th>工序分摊单价</th>
                    </tr>
                    </thead>
                    <tbody id="gxMxTbdy">
                    {if $detail.sharType==2}
                    {volist name='csddList' id='cdl' key='cdlkey'}
                    <tr>
                        <td><span class="xuhao">{$cdlkey} </span>
                            <input type='hidden' name='num[]' value='{$cdl.num}'>
                            <input type='hidden' name='craft_detail_id[]' value='{$cdl.craft_detail_id}'>
                        </td>
                        <td>{$cdl.craft_name}</td>
                        <td>{$cdl.num}</td>
                        <td>{$cdl.costPrice}</td>
                        <td>{$cdl.gxPrice}</td>
                    </tr>
                    {/volist}
                    {/if}
                    </tbody>


                </table>

            </tr>
            <tr> <h3>分摊明细</h3></tr>
            <tr>
                <table class="layui-table ftMxTable">
                    <thead>
                    <tr>
                        <th>序号</th>
                        <th>费用名称</th>
                        <th>费用金额</th>
<!--                        <th>操作<a href="javascript:" class='layui-btn layui-btn-normal layui-btn-xs' onclick='addFtMx()'>添加</a></th>-->
                    </tr>
                    </thead>
                    <tbody id="ftMxTbdy">
                    {if isset($csdList)}
                    {volist name='csdList' id='csdl' key='csdk'}
                    <tr>

                        <td><span class="mxXuhao">{$csdk}</span>
                            <input type="hidden" name="ftmxid[]" value="{$csdl.id}">
                        </td>
                        <td><input type="text" name="ftName[]" value="{$csdl.name}" class="layui-input" required placeholder="费用名称"></td>
                        <td>
                            <input type="text" name="ftNum[]" value="{$csdl.price}" class="layui-input" required placeholder="费用金额" onkeyup="digitalFiltering(this,2)" >
                        </td>
                        <td>
<!--                            {if !isset($detail.status)||$detail.status==1}-->
<!--                            <a href="javascript:" class='layui-btn layui-btn-danger layui-btn-xs ' onclick="delFtMx('{$csdl.id}',this)">删除</a>-->
<!--                            {/if}-->
                        </td>
                    </tr>
                    {/volist}
                    {else}
                    <tr>
                   
                        <td><span class="mxXuhao">1</span>
                            <input type="hidden" name="ftmxid[]" value="">
                    </td>
                        <td><input type="text" name="ftName[]" class="layui-input" required placeholder="费用名称"></td>
                        <td>
                            <input type="text" name="ftNum[]" class="layui-input" required placeholder="费用金额" onkeyup="digitalFiltering(this,2)" >
                        </td>
                        <td><a href="javascript:" class='layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled' >删除</a></td>
                    </tr>
                    {/if}
                    </tbody>
                </table>
            </tr>
        </table>
        <div class="py-3">
            {if isset($detail.status)&&$detail.status==1}
            <button type="button" class="layui-btn layui-btn-normal" onclick="onSure()">确认</button>
            {/if}
        </div>
    </form>
</div>

{/block}
<!-- /主体 -->
{block name="script"}
<script>

    const moduleInit = ['tool'];

    let isSub = 2;
    let sharType=0;
    let shareDate="{$detail.sharTime|default=''}";
    function gouguInit() {
        var form = layui.form, tool = layui.tool;
        //监听提交
        form.on('submit(webform1)', function (data) {
            let callback = function (e) {
                isSub = 2;
                layer.msg(e.msg);
                if (e.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            }
            if (isSub == 2) {
                isSub = 1
                tool.post("{:url('add')}", data.field, callback);
            }
            return false;
        });

        // layui.use(['form'], function () {
        //     var form = layui.form;
        //     form.render();
        // });
        form.on('select(sharType)', function (data) {
            sharType=data.value;
            if (data.value==2){
                $("#scNum").css('display','none')
                $("#scMxTbdy").empty();
                $("#gxNum").css('display','')
                $("#gxWl").css('display','')
                $("#scWl").css("display",'none')
            }else {
                $("#scNum").css('display','')
                $("#gxNum").css('display','none')
                $("#gxMxTbdy").empty();
                $("#gxWl").css('display','none')
                $("#scWl").css("display",'')
            }
        });
        //动态获取联动
        //弹窗
        var treeTable = layui.table, form = layui.form;
        var tableIns = treeTable.render({
            elem: '#mxTable'
            , url: "{:url('addMx')}"
            , method: 'post'
            , toolbar: '#toolbarDemo',
            defaultToolbar: []
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left'},
                {fixed: 'left', field: 'id', title: 'ID', unresize: true, hide: true},
                {field: 'id', width: 80, title: 'ID号', align: 'center'}
                , {field: 'specs', title: '规格型号'}
                , {field: 'mdName', title: '名称'}
                , {field: 'bar_code', title: '编码'}
                , {field: 'unit', title: '单位'}
            ]]
            , page: true
        });
        //监听搜索提交
        form.on('submit(webform)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        //表头工具栏事件
        treeTable.on('toolbar(mxTable)', function (obj) {
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch (obj.event) {
                case 'add':
                    break;
                case 'subt':
                    var data = checkStatus.data;
                    var ids = []
                    if (data.length) {
                        data.forEach(function (value, index) {
                            ids.push(value)
                        })
                        saveAddMx(ids)
                        layer.close(layTip)

                    } else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':

                    break;

            }
            ;
        })
        laydate.render({
            elem: '#orderDate'
            , type: 'date'
        });
        laydate.render({
            elem: '#sharTime'
            , type: 'date',
            range:'~',
            done(res){
                shareDate=res
            }
        });
    }
</script>
<script>
    function getGxWL() {
        if (shareDate==''){
            layer.msg('请选择分摊时间')
            return
        }
        $.ajax({
            url:"{:url('api/getFtMx')}",
            data:{shareDate:shareDate},
            type:'post',
            dataType:'json',
            success:function (res) {
                if (res.code!=0){
                    layer.msg(res.msg);
                }else {
                    $("#gxMxTbdy").empty()
                    let data=res.data;
                    let str='';
                    for (var i=0;i<data.length;i++){
                      let item=data[i];
                      str+="  <tr>\n" +
                          "                        <td><span class=\"xuhao\">"+ parseInt(i+1) +"" +
                          "<input type='hidden' name='num[]' value='"+item.num+"'><input type='hidden' name='craft_detail_id[]' value='"+item.craft_detail_id+"'>" + "</span>\n" +
                          "                        <td>"+item.craftName+"</td>\n" +
                          "                        <td>"+item.num+"</td>\n" +
                          "                        <td>\n" +
                          "                            <a href=\"javascript:\" class=\"layui-btn layui-btn-xs layui-btn-danger\"\n" +
                          "                               onclick=\"removetr(this)\">删除</a>\n" +
                          "                        </td>\n" +
                          "                    </tr>"
                    }
                    $("#gxMxTbdy").append(str)
                }
            },
            error(){
                layer.msg('网络错误，请稍后再试')
            }
        })
    }

    function saveAddMx(ojb) {
        $("#scMxTbdy").empty();
        let str = '';
        for (var i = 0; i < ojb.length; i++) {
            str += "  <tr>\n" +
                "                <td> <span class=\"xuhao\">" + parseInt(i+1) + "</span>" +
                "<input type=\"hidden\" name=\"material_id[]\" value=\"" + ojb[i].material_dossier_id + "\">" +
                "<input type=\"hidden\" name=\"in_receipt_detail_id[]\" value=\"" + ojb[i].id + "\">" +
                "<input type=\"hidden\" name=\"num[]\" value=\"" + ojb[i].inNum + "\">" +
                "</td>\n" +
                "                <td> " + ojb[i].bar_code + " </td>\n" +
                "                <td> " + ojb[i].mdName + " </td>\n" +
                "                <td>" + ojb[i].specs + " </td>\n" +
                "                <td>" + ojb[i].unit + " </td>\n" +
                "                <td>" + ojb[i].inNum + " </td>\n" +
                "                <td>\n" +
                "                    <a href=\"javascript:\" class='layui-btn layui-btn-danger layui-btn-xs' onclick='removetr(this)'>删除</a>\n" +
                "                </td>\n" +
                "            </tr>";
        }
        $("#scMxTbdy").append(str)
        layui.use(['form','laydate'], function () {
            var form = layui.form;
            var laydate = layui.laydate;
            laydate.render({
                elem: '.planned_time'
                , type: 'date',
            });
            form.render();
        });
    }
    function removetr(e) {
        $(e).parent().parent().remove()
    }
    function addFtMx() {
        var lastListItem = $('.ftMxTable tr:last');
        let aa=$(lastListItem).find(".mxXuhao")
        let len=$(aa).text()
        console.log('leng:',len)
        if (!parseInt(len)){
            len=0;
        }
        let index=parseInt(len)+1;
        let tr='   <tr>\n' +
            '                   \n' +
            '                        <td><span class="mxXuhao">'+index+'</span>\n' +
            '                            <input type="hidden" name="ftmxid[]" value="">\n' +
            '                    </td>\n' +
            '                        <td><input type="text" name="ftName[]" class="layui-input" required placeholder="费用名称"></td>\n' +
            '                        <td>\n' +
            '                            <input type="text" name="ftNum[]" class="layui-input" required placeholder="费用金额" onkeyup="digitalFiltering(this,2)" >\n' +
            '                        </td>\n' +
            '                        <td><a href="javascript:" class=\'layui-btn layui-btn-danger layui-btn-xs\' onclick=\'removetr(this)\'>删除</a></td>\n' +
            '                    </tr>'
        $("#ftMxTbdy").append(tr)
        gouguInit()
    }
    function delMx(id = '', e) {
        if (id == '') {
            layer.msg('请选择数据')
            return;
        }
        $.ajax({
            url: "{:url('removeMx')}",
            type: 'post',
            dataType: 'json',
            data: {id: id},
            success: function (res) {
                if (res.code != 0) {
                    layer.msg(res.msg)
                } else {
                    removetr(e)
                }
            },
            error(e) {
                layer.msg('网络错误，请稍后再试')
            }
        })
    }

    function delFtMx(id = '', e) {
        if (id == '') {
            layer.msg('请选择数据')
            return;
        }
        $.ajax({
            url: "{:url('removeFtMx')}",
            type: 'post',
            dataType: 'json',
            data: {id: id},
            success: function (res) {
                if (res.code != 0) {
                    layer.msg(res.msg)
                } else {
                    removetr(e)
                }
            },
            error(e) {
                layer.msg('网络错误，请稍后再试')
            }
        })
    }
    let is_sure=2;
    function onSure() {
        let id="{$id}";
        if (id==0){
            layer.msg('操作有误');
            return;
        }
        if (is_sure==1){
            return;
        }
        is_sure=1;
        $.ajax({
            url:"{:url('onSure')}",
            type:'post',
            dataType:'json',
            data:{id:id},
            success:function (res) {
                is_sure=2;
                layer.msg(res.msg)
                if (res.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            },
            error(e){
                layer.msg('网络错误，请稍后重试')
                is_sure=2
            }
        })
    }
    let is_sub=2;
    function onSub() {
        let id="{$id}";
        if (id==0){
            layer.msg('操作有误');
            return;
        }
        if (is_sub==1){
            return;
        }
        is_sub=1;
        $.ajax({
            url:"{:url('confirm')}",
            type:'post',
            dataType:'json',
            data:{id:id},
            success:function (res) {
                is_sub=2;
                layer.msg(res.msg)
                if (res.code == 0) {
                    setTimeout(function () {
                        parent.parent.location.reload();
                    }, 1000);
                }
            },
            error(e){
                layer.msg('网络错误，请稍后重试')
                is_sub=2
            }
        })
    }
</script>
{/block}
<!-- /脚本 -->